﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using TransformerManager.Shared.Dto.Transformer;
using TransformerManager.Shared.Helper.HttpParams;
using TransformerManager.Shared.ResourceParameters;
using TransformerManager.WPF.Common.Helper;
using hc = HandyControl.Controls;

namespace TransformerManager.WPF.Common.Http.Service
{
    public class TransformerService : ITransformerService
    {
        private readonly HttpRequest _httpRequest;
        public TransformerService(HttpRequest httpRequest)
        {
            _httpRequest = httpRequest;
        }

        public async Task<ServiceResponse<TransformerDto>> AddTransformerAsync(TransformerDto transformerDto)
        {
            string url = $"Transformers";
            string body = JsonConvert.SerializeObject(transformerDto);
            HttpResponseMessage responseMsg = await _httpRequest
                .RequestAsync(HttpMethod.Post, url, null, body);
            string content = await responseMsg.Content.ReadAsStringAsync();
            ServiceResponse<TransformerDto> result = 
                JsonConvert.DeserializeObject<ServiceResponse<TransformerDto>>(content);

            return result;
        }

        public async Task<bool> CheckProjectNumberExistAsync(string projectNumber)
        {
            string url = $"Transformers/{projectNumber}";
            var responseMsg = await _httpRequest.RequestAsync(HttpMethod.Get, url, null);
            var content = await responseMsg.Content.ReadAsStringAsync();
            var result = JsonConvert.DeserializeObject<ServiceResponse<bool>>(content);

            if (result != null && !result.Data)
            {
                hc.Growl.Error(
                        $"响应信息: {result.Message}",
                        "Notification");
                return result.Data;
            }
            return result.Data;
        }

        public async Task<ServiceResponse> DeleteTransformerByIdAsync(Guid id)
        {
            string url = $"Transformers/{id}";
            // 获取响应
            HttpResponseMessage responseMsg = await _httpRequest.RequestAsync(HttpMethod.Delete, url, null);
            
            ServiceResponse result = new ServiceResponse();
            string content = await responseMsg.Content.ReadAsStringAsync();
            if (string.IsNullOrEmpty(content))
            {
                result.Success = false;
                result.Message = StatusCodeDescription.GetDescription(responseMsg.StatusCode);
                return result;
            }
            result = JsonConvert.DeserializeObject<ServiceResponse>(content);
            return result;
        }

        public async Task<TransformerDto> GetTransformerInfoById(Guid id)
        {
            string url = $"Transformers/{id}";
            // 获取响应
            var responseMsg = await _httpRequest
                .RequestAsync(HttpMethod.Get, url, null);
            var content = await responseMsg.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject<ServiceResponse<TransformerDto>>(content);
            if (!result.Success)
            {
                hc.Growl.Error(
                        $"响应信息: {result.Message}",
                        "Notification");
                return result.Data;
            }

            return result.Data;
        }

        public async Task<HeaderResponse<List<TransformerDto>>> GetTransformersAsync(TransformerResourceParameters transformerResource, int pageIndex, int pageSize)
        {
            string url = $"Transformers/Query?pageNumber={pageIndex}&pageSize={pageSize}";
            string body = JsonConvert.SerializeObject(transformerResource);
            var responseMsg = await _httpRequest.RequestAsync(HttpMethod.Post, url, null, body);
            var content = await responseMsg.Content.ReadAsStringAsync();
            var headers = responseMsg.Headers;
            var result = JsonConvert.DeserializeObject<ServiceResponse<List<TransformerDto>>>(content);
            if (!result.Success)
            {
                hc.Growl.Error(
                        $"响应信息: {result.Message}",
                        "Notification");
                return new HeaderResponse<List<TransformerDto>>
                {
                    Data = new List<TransformerDto>(),
                    Headers = headers
                };
            }

            return new HeaderResponse<List<TransformerDto>>
            {
                Data = result.Data,
                Headers = headers
            };
        }

        public async Task<int> GetTransformersNumByRegionAsync(string region, DateTime from, DateTime end)
        {
            string url = $"Transformers/Number?regionType={region}&from={from:yyyy/MM/dd}&end={end:yyyy/MM/dd}";

            var responseMsg = await _httpRequest.RequestAsync(HttpMethod.Get, url, null);
            var content = await responseMsg.Content.ReadAsStringAsync();
            var result = JsonConvert.DeserializeObject<ServiceResponse<int>>(content);

            if (!result.Success)
            {
                hc.Growl.Error(
                        $"响应信息: {result.Message}",
                        "Notification");
                return result.Data;
            }
            return result.Data;
        }

        public async Task<ServiceResponse> UpdateTransformerInfoByIdAsync(Guid Id, TransformerDto transformerDto)
        {
            // 请求数据
            string url = $"Transformers/{Id}";
            string body = JsonConvert.SerializeObject(transformerDto);
            // 获取响应
            var responseMsg = await _httpRequest.RequestAsync(HttpMethod.Put, url, null, body);

            ServiceResponse result = new ServiceResponse();
            string content = await responseMsg.Content.ReadAsStringAsync();
            if (string.IsNullOrEmpty(content))
            {
                result.Success = false;
                result.Message = StatusCodeDescription.GetDescription(responseMsg.StatusCode);
                return result;
            }
            result = JsonConvert.DeserializeObject<ServiceResponse>(content);
            return result;
        }
    }
}
